package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.audit.baidu.BaiduImageScan;
import com.heima.audit.baidu.BaiduTextScan;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private BaiduImageScan baiduImageScan;

    @Autowired
    private BaiduTextScan baiduTextScan;


    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public ResponseResult upload(MultipartFile file) throws IOException {

        //TODO
        String originalFilename = file.getOriginalFilename();
            String prefix = "192.168.200.128:9000/leadnews";
            InputStream inputStream = file.getInputStream();
            String url = fileStorageService.uploadImgFile(prefix, originalFilename, inputStream);
        byte[] bytes = fileStorageService.downLoadFile(url);
        Integer integer = baiduImageScan.imageScan(bytes);
        if (integer==1){
            WmUser user = WmThreadLocalUtil.getUser();
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(url);
            wmMaterial.setUserId(user.getId());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setCreatedTime(new Date());
            wmMaterialMapper.insert(wmMaterial);

            return ResponseResult.okResult(wmMaterial);
        }
        return ResponseResult.errorResult(501,"图片不合法");



    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult list(WmMaterialDto dto) {
       //TODO
        PageHelper.startPage(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dto.getIsCollection()==1, WmMaterial::getIsCollection, dto.getIsCollection());
        List<WmMaterial> wmMaterials = wmMaterialMapper.selectList(lambdaQueryWrapper);
        PageInfo<WmMaterial> pageInfo = new PageInfo<>(wmMaterials);
        long total = pageInfo.getTotal();
        int pageNum = pageInfo.getPageNum();
        int size = pageInfo.getSize();
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setCurrentPage(pageNum);
        pageResponseResult.setTotal((int) total);
        pageResponseResult.setSize(size);
        pageResponseResult.setHost("null");
        pageResponseResult.setCode(200);
        pageResponseResult.setData(wmMaterials);

        return pageResponseResult ;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        //TODO
        wmMaterialMapper.selectById(id);
        wmMaterialMapper.deleteById(id);
        LambdaQueryWrapper<WmNewsMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmNewsMaterial::getMaterialId, id);
        List<WmNewsMaterial> wmNewsMaterials = wmNewsMaterialMapper.selectList(lambdaQueryWrapper);
        if (wmNewsMaterials.size() > 0){
            return ResponseResult.errorResult(501,"素材已被使用，不能删除");
        }
        return ResponseResult.okResult(200,"删除成功");
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel(Integer id) {
        //TODO
        LambdaUpdateWrapper<WmMaterial> wmMaterialLambdaQueryWrapper = new LambdaUpdateWrapper<>();
        wmMaterialLambdaQueryWrapper.eq(WmMaterial::getId, id);
        wmMaterialLambdaQueryWrapper.set(WmMaterial::getIsCollection, (short) 0);
        wmMaterialMapper.update(new WmMaterial(),wmMaterialLambdaQueryWrapper);
        return ResponseResult.okResult(200,"取消收藏成功");

    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        //TODO
        //构建修改收藏条件
        LambdaUpdateWrapper<WmMaterial> wmMaterialLambdaQueryWrapper = new LambdaUpdateWrapper<>();
        wmMaterialLambdaQueryWrapper.eq(WmMaterial::getId, id);
        wmMaterialLambdaQueryWrapper.set(WmMaterial::getIsCollection, (short) 1);
        wmMaterialMapper.update(new WmMaterial(),wmMaterialLambdaQueryWrapper);
        return ResponseResult.okResult(200,"收藏成功");
    }
}
